<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/21
 * Time: 17:49
 */

namespace app\model\user;


use app\common\lib\Common;
use app\model\BaseModel;

class UserModel extends BaseModel
{
    protected $table = 'video_user';

    protected $primaryKey = 'userid';

    protected $headimgurl = 'https://ai-video-1320718644.cos.ap-guangzhou.myqcloud.com/video/test/image/20240401/png/78c8502563218baccd80.png';

    protected static $instance;

    public $timestamps = false;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function insertData($param)
    {
        $maxId =  $this->getWhere([['user_sn','>',0]])->max('user_sn');
        $userSn = Common::getSnCode($maxId,'20',6, 2);

        $insert = [
            'user_sn' => $userSn,
            'mobile' => $param['mobile'],
            'nickname' => $param['nickname'],
            'headimgurl' => $param['headimgurl']??$this->headimgurl,
            'scene' => $param['scene']??'',
            'invit_code' => $param['invit_code']??'',
            'qrcode_image' => $param['qrcode_image']??'',
            'parentid' => $param['parentid']??'',
            'user_role' => $param['user_role']??'0',
            'device_number' => request()->deviceNumber??'',
            'uuid' =>  $param['uuid']??(request()->header('uuid')??''),
            'source_ip' => request()->getRealIp(),
            'version' => request()->version,
            'weixin_unionid' => $param['weixin_unionid']??'',
            'ios_unionid' => $param['ios_unionid']??'',
            'google_unionid' => $param['google_unionid']??'',

            'created_at' => $param['createtime']??date('Y-m-d H:i:s'),
            'updated_at' => $param['createtime']??date('Y-m-d H:i:s'),
        ];

        $userId = $this->insertGetId($insert);

        return $userId;
    }

    /**
     * 递归调用更新邀请人数
     * @param $userSn
     * @param $parentid
     * @param $level
     * @return bool
     */
    public function updateInviteUserNums($userSn,$parentid,$level=1)
    {
        if($userSn==$parentid){
            return false;
        }
        if(!empty($parentid)){
            $parentUser = $this->getWhere(['user_sn'=>$parentid])->first();
            if($parentUser){
                //插入邀请关系
                UserInviteRelationModel::getInstance()->updateOrInsert(
                    [
                        'user_sn'=>$parentid,
                        'invite_user_sn'=>$userSn
                    ],
                    [
                        'level'=>$level,
                    ]
                );
                $parentUser->invit_user_nums = (int)(UserInviteRelationModel::getInstance()->getWhere(['user_sn'=>$parentid,['level','<',3]])->count());
                $parentUser->invit_one_user_nums = (int)(UserInviteRelationModel::getInstance()->getWhere(['user_sn'=>$parentid,['level','=',1]])->count());
                $parentUser->invit_two_user_nums = (int)(UserInviteRelationModel::getInstance()->getWhere(['user_sn'=>$parentid,['level','=',2]])->count());
                $parentUser->save();

                if($parentUser['parentid']>0){
                    $level++;
                    return $this->updateInviteUserNums($userSn,$parentUser['parentid'],$level);
                }
            }
        }

        return true;
    }

    public function getUserAgentVip($userSn,$dateTime=null)
    {
        $dateTime==false && $dateTime = date('Y-m-d');

        $agentLevel = false;
//        $where = ['user_sn'=>$userSn,['effect_time','<=',$dateTime],['expire_time','>=',$dateTime],['status','>',0]];
//        $agentLevel = UserLevelChangeModel::getInstance()->getWhere($where)->orderBy('createtime','desc')->value('change_after_level');
        if($agentLevel==false){
            $agentLevel = $this->getWhere(['user_sn'=>$userSn])->value('agent_level');
        }

        return $agentLevel?:1;
    }
}